//
//  ViewController.m
//  上传
//
//  Created by student on 16/3/30.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

#define kBoundary @"niit123456"

#define kNewLine @"\r\n"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self uploadFile];
    
}

-(void)uploadFile
{
    //请求的网址
    NSURL *url=[NSURL URLWithString:@"http://192.168.13.11:8080/MJServer/upload"];
    
    //请求
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    
    //POST
    request.HTTPMethod=@"POST";
    
    //设置请求头
    NSString *headerStr=[NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBoundary];
    [request setValue:headerStr forHTTPHeaderField:@"Content-Type"];
    
    //请求体
    NSMutableData *mData=[NSMutableData data];
    NSMutableString *mStr=[NSMutableString string];
    
    //1.文件参数
    [mStr appendString:kNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"file\"; filename=\"1.txt\""];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Type: text/plain"];
    [mStr appendString:kNewLine];
    [mStr appendString:kNewLine];
    // NSLog(@"%@",mStr);
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    //文件的数据
    NSString *filepath=[[NSBundle mainBundle] pathForResource:@"1" ofType:@"txt"];
    NSData *data=[NSData dataWithContentsOfFile:filepath];
    [mData appendData:data];
    
    //其他参数
    mStr = [NSMutableString string];
    [mStr appendString:kNewLine];
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:kNewLine];
    [mStr appendString:@"Content-Disposition: form-data; name=\"username\""];
    [mStr appendString:kNewLine];
    [mStr appendString:kNewLine];
    [mStr appendString:@"abc"];
    [mStr appendString:kNewLine];
    
    //结尾
    [mStr appendString:@"--"];
    [mStr appendString:kBoundary];
    [mStr appendString:@"--"];
    [mStr appendString:kNewLine];
    // NSLog(@"%@",mStr);
    [mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    request.HTTPBody=mData;
    
    //发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
    {
        
        if (connectionError==nil)
        {
            NSError *error;
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
            
            if (error!=nil)
            {
                NSLog(@"%@",dict);
            }
            else
            {
                NSLog(@"%@",[error localizedDescription]);

            }
        }
        else
        {
           NSLog(@"%@",[connectionError localizedDescription]);  

        }
    }];
}


@end
